package es.unileon.is.finalproyect.userinterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.*;

import es.unileon.is.finalproyect.temporizer.Temporizer;
import es.unileon.is.finalproyect.temporizer.TimeLabel;
import es.unileon.is.finalproyect.texts.Question;
import es.unileon.is.finalproyect.texts.Text;
import es.unileon.is.finalproyect.users.User;

public class ResolveTextInterface extends JDialog {

	private List<Question> _questions = new ArrayList<Question>();
	private List<JRadioButton> _buttons = new ArrayList<JRadioButton>();
	private User _user;
	private Text _text;
	private JButton _okButton;
	
	public ResolveTextInterface(Text t, User u, JFrame parent, boolean modal) {
		super(parent, modal);
		_user = u;
		_text=t;
		setPreferredSize(new Dimension(800,600));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		//*****modificaciones para JAVAHELP*******
		JMenuBar menuBar = new JMenuBar();
		JMenu menuHelp = new JMenu("Help");
		JMenuItem help = new JMenuItem("Show help");
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(menuHelp);
		menuHelp.add(help);
		setJMenuBar(menuBar);
		//metodo para dar la accion a help
		ViewHelp.showHelp(help, getContentPane(), "ventana_resolucion-texto");
		//*****************************************
				
		getContentPane().setLayout(new BorderLayout());
		_questions=t.get10RandomQuestion();
		//Dentro del panel habra un ScrollPane y dentro de el iran los labels y los RadioButton.
		JPanel panelScroll = new JPanel(null);
		JScrollPane scroll = new JScrollPane(panelScroll);
		scroll.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
		getContentPane().add(scroll);
		int y=10;
		for(Question q: _questions){
			String question = q.getQuestion();
			String[] answers = q.getAnswers();
			//Colocamos la pregunta
			JLabel title = new JLabel(question);
			int titleWidth = (int) title.getPreferredSize().getWidth();
			int titleHeight = (int) title.getPreferredSize().getHeight();
			title.setBounds(50,y,titleWidth, titleHeight);
			panelScroll.add(title);
			y+=titleHeight+10;
			//Obtenemos las respuestas
			ButtonGroup bg = new ButtonGroup();
			List<JRadioButton> temp = new ArrayList<JRadioButton>();
			for(int i=0;i<answers.length;i++){
				JRadioButton radioButton = new JRadioButton(answers[i]);
				temp.add(radioButton);
				bg.add(radioButton);
			}
			_buttons.addAll(temp);
			//Barajamos las respuestas
			for(int i = temp.size(); i >0; i--){
				int random = (int) (Math.random()*i);
				JRadioButton radio = temp.remove(random);
				int radioWidth = (int) radio.getPreferredSize().getWidth();
				int radioHeight = (int) radio.getPreferredSize().getHeight();
				radio.setBounds(70,y,radioWidth, radioHeight);
				panelScroll.add(radio);
				y+=radioHeight+10;
			}	
		}	
		panelScroll.setPreferredSize(new Dimension(500,y));
		
		//Derecha: contador de tiempo y boton enviar (queda el contador y el actionlistener de enviar).
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		
		_okButton = new JButton("Send");
		
		final Temporizer clock = new Temporizer(_text.getSecondsToAnswer(), _okButton);
		TimeLabel timeLabel = new TimeLabel(clock);
		
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(timeLabel);
		buttonPane.add(Box.createRigidArea(new Dimension(20,0)));
		buttonPane.add(_okButton);
		
		
		_okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				clock.setDone();
				Map<Question,Integer> map = new HashMap<Question,Integer>();
				for(int i=0;i<_questions.size();i++){
					Question q = _questions.get(i);
					for(int j=0;j<4;j++){	
						JRadioButton bt = _buttons.get(4*i+j);
						if(bt.isSelected()){
							map.put(q,j+1);
							break;
						} else if(j == 3){
							map.put(q,0);
						}
					}//fin de for j
				}//fin de for i
				_user.resolveText(_text, map);
				dispose();
				new TextReviewInterface(_text,_user).setVisible(true);
			}
		});
			
		pack();
		clock.start();
	}


}
